package com.sunshine.androidjavapractise.test;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public final class ThreadManager {
    private final BlockingQueue<Runnable> mWorkQueue;
    private ExecutorService mExecutor;
    private static final int MAX_CORE_NUMBER = Runtime.getRuntime().availableProcessors();

    private ThreadManager() {
        mWorkQueue = new LinkedBlockingDeque<>();
        mExecutor = new ThreadPoolExecutor(MAX_CORE_NUMBER, MAX_CORE_NUMBER, 1, TimeUnit.SECONDS, mWorkQueue);
    }

    private static ThreadManager sInstance;

    public static ThreadManager getsInstance() {
        if (null == sInstance) {
            synchronized (ThreadManager.class) {
                if (null == sInstance) {
                    sInstance = new ThreadManager();
                }
            }
        }
        return sInstance;
    }

    public void excute(Runnable runnable) {
        mExecutor.execute(runnable);
    }
}
